package cn.jingzhuan.fm.ui.activity

import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import cn.jingzhuan.element.demo.view.util.statusbar.StatusBarCompat
import cn.jingzhuan.fm.R
import cn.jingzhuan.fm.databinding.ActivityFmAudioBinding
import cn.jingzhuan.fm.databinding.ViewFmAudioInfoBinding
import cn.jingzhuan.fm.databinding.ViewFmAudioMoreBinding
import cn.jingzhuan.fm.mvp.contract.FMAudioContract
import cn.jingzhuan.fm.mvp.presenter.FMAudioPresenter
import cn.jingzhuan.fm.pojo.FMAlbum
import cn.jingzhuan.fm.pojo.FMAudio
import cn.jingzhuan.fm.ui.fragment.FMAudioPlayListFragment
import cn.jingzhuan.fm.utils.ShareUtil
import com.bilibili.socialize.share.core.SocializeListeners
import com.bilibili.socialize.share.core.SocializeMedia
import javax.inject.Inject


class FMAudioActivity:BaseBindingActivity<ActivityFmAudioBinding>() , FMAudioContract.View{

    @Inject
    lateinit var fmAudioPresenter : FMAudioPresenter
    private lateinit var infoBinding : ViewFmAudioInfoBinding
    private lateinit var moreBinding: ViewFmAudioMoreBinding
    lateinit var album: FMAlbum
    lateinit var audio: FMAudio

    companion object {
        fun startActivity(context: Context, audioId:String) {
            var intent = Intent(context,FMAudioActivity::class.java)
            intent.putExtra("audioId",audioId)
            context.startActivity(intent)
        }
    }

    override fun onDataBind(savedInstanceState: Bundle?, binding: ActivityFmAudioBinding?) {
        initView()
        initListener()
        initData()
    }

    private fun initListener() {
        // 点击播放列表按钮
        this.binding.audioPlayList.setOnClickListener {
            var playListFragment = FMAudioPlayListFragment.newInstance(album.id)
            playListFragment.show(supportFragmentManager,"fmPlayList")
        }


        // 点击查看内容详情
        this.binding.audioDocument.setOnClickListener {
            FMAudioDocumentActivity.startActivity(this.baseContext,audio,album)
        }

        // 点击查看留言/评论
        this.binding.audioMessage.setOnClickListener {
            FMAudioCommentActivity.startActivity(this.baseContext,audio.id)
        }

        // 点击分享
        this.binding.audioShare.setOnClickListener {
            ShareUtil.shareUrl(this, getString(R.string.app_name), audio.name, audio.url,
                object : SocializeListeners.ShareListener {
                    override fun onSuccess(type: SocializeMedia?, code: Int) {
                        Toast.makeText(baseContext, "分享成功", Toast.LENGTH_SHORT).show()
                    }

                    override fun onCancel(type: SocializeMedia?) {
                    }

                    override fun onProgress(type: SocializeMedia?, progressDesc: String?) {
                    }

                    override fun onError(type: SocializeMedia?, code: Int, error: Throwable?) {
                        Toast.makeText(baseContext, "分享失败", Toast.LENGTH_SHORT).show()
                    }

                    override fun onStart(type: SocializeMedia?) {
                    }

                })
        }

    }

    fun initData() {
        var audioId = intent.getStringExtra("audioId")
        fmAudioPresenter.audioFind(audioId)
    }

    fun initView() {
        var views = mutableListOf<View>()
        infoBinding = DataBindingUtil.inflate(layoutInflater, R.layout.view_fm_audio_info, this.binding.viewPager, false)
        moreBinding = DataBindingUtil.inflate(layoutInflater, R.layout.view_fm_audio_more, this.binding.viewPager, false)
        views.add(infoBinding.root)
        views.add(moreBinding.root)
        this.binding.viewPager.adapter = object : PagerAdapter() {
            override fun isViewFromObject(view: View, obj: Any): Boolean = (view==obj)
            override fun getCount(): Int = views.size
            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                var view = views[position]
                container.addView(view)
                return view
            }
            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(views.get(position))
            }
        }
        this.binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {}

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {
                binding.viewPagerIndicator.setSelected(position)
            }
        })
        this.binding.viewPagerIndicator.count = views.size
        this.binding.emptyView.show(true)
        StatusBarCompat.translucentStatusBar(this,true)
        this.binding.seekBar.setPadding(0,0,0,0)
    }

    override fun getLayoutId(): Int = R.layout.activity_fm_audio


    override fun bindData(fmAudio: FMAudio, fmAlbum: FMAlbum) {
        this.binding.audio = fmAudio
        this.binding.album = fmAlbum
        this.infoBinding.name = fmAudio.name
        this.infoBinding.imageUrl = fmAlbum.logoUrl
        this.infoBinding.listenCount = fmAudio.listenCount
        this.album = fmAlbum
        this.audio = fmAudio

        this.binding.emptyView.hide()
    }
}